/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.template.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.msgsms.v2.MsgsmsClient;
import com.huaweicloud.sdk.msgsms.v2.model.CreateTemplateRequest;
import com.huaweicloud.sdk.msgsms.v2.model.CreateTemplateResponse;
import com.huaweicloud.sdk.msgsms.v2.model.DeleteTemplateRequest;
import com.huaweicloud.sdk.msgsms.v2.model.DeleteTemplateResponse;
import com.huaweicloud.sdk.msgsms.v2.model.ListSendCountryDetailsRequest;
import com.huaweicloud.sdk.msgsms.v2.model.ListSendCountryDetailsResponse;
import com.huaweicloud.sdk.msgsms.v2.model.ShowTemplateRequest;
import com.huaweicloud.sdk.msgsms.v2.model.ShowTemplateResponse;
import com.huaweicloud.sdk.msgsms.v2.model.SmsTemplateReq;
import com.huaweicloud.sdk.msgsms.v2.model.SmsTemplateVariableAttrReq;
import com.huaweicloud.sdk.msgsms.v2.region.MsgsmsRegion;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 提供创建短信模板、获取模板详情、删除短信模板样例，包含创建短信模板时需获取发送国家的样例。
 */
public class MsgsmsTemplateManageDemo {
    public static void main(String[] args) {
        System.out.println("Start HUAWEI CLOUD MSGSMS Template Manage Java Demo...");

        ICredential auth = getCredential("YOUR AK", "YOUR SK","YOUR PROJECT ID");

        // 创建MsgsmsClient实例
        MsgsmsClient client = getClient(MsgsmsRegion.CN_NORTH_4, auth);

        try {
            Long countryId = querySendCountryId(client);
            System.out.println("countryId: " + countryId);
            String id = createTemplate(client, "YOUR APPID", "YOUR SIGNATURE KEY ID", countryId);
            showTemplate(client, id);
            deleteTemplate(client, id);

        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    /**
     * 查询发送国家
     * @param client MsgsmsClient实例
     * @return 国家ID
     */
    private static Long querySendCountryId(MsgsmsClient client) {
        ListSendCountryDetailsRequest request = new ListSendCountryDetailsRequest();
        request.setCountryNameEn("China");

        // 调用SDK的接口获取发送国家
        ListSendCountryDetailsResponse response = client.listSendCountryDetails(request);
        System.out.println(response.toString());
        return response.getBody().get(0).getCountryId().longValue();
    }

    /**
     * 创建短信模板
     * @param client MsgsmsClient实例
     * @param appId 应用ID
     * @param signId 应用签名主键ID
     * @param countryId 国家ID
     * @return 签名主键ID
     */
    private static String createTemplate(MsgsmsClient client, String appId, String signId, Long countryId) {
        CreateTemplateRequest request = new CreateTemplateRequest();
        SmsTemplateReq body = new SmsTemplateReq();
        body.setAppId(appId);
        body.setSignId(signId);
        body.setRegion("cn");
        body.setSendCountry(new ArrayList<>(Collections.singletonList(countryId)));
        body.setTemplateName("myTestTemplate");
        body.setTemplateType("VERIFY_CODE_TYPE");
        body.setBrackets("GB");
        body.setTemplateContent("template ${1} test.");
        body.setUniversalTemplate(1);
        SmsTemplateVariableAttrReq var = new SmsTemplateVariableAttrReq();
        var.setVariableIndex(1);
        var.setVariableType("TEXT");
        var.setVariableDesc("var1");
        body.setVariableAttributes(new ArrayList<>(Collections.singletonList(var)));
        request.withBody(body);

        // 调用SDK的接口创建短信模板
        CreateTemplateResponse response = client.createTemplate(request);
        System.out.println(response.toString());
        return response.getId();
    }

    /**
     * 获取模板详情
     * @param client MsgsmsClient实例
     * @param id 模板ID
     */
    private static void showTemplate(MsgsmsClient client, String id) {
        ShowTemplateRequest request = new ShowTemplateRequest();
        request.setId(id);

        // 调用SDK的接口查询模板详情
        ShowTemplateResponse response = client.showTemplate(request);
        System.out.println(response.toString());
    }

    /**
     * 删除短信模板
     * @param client MsgsmsClient实例
     * @param id 模板ID
     */
    private static void deleteTemplate(MsgsmsClient client, String id) {
        DeleteTemplateRequest request = new DeleteTemplateRequest();
        request.setId(id);

        // 调用SDK的接口删除短信模板
        DeleteTemplateResponse response = client.deleteTemplate(request);
        System.out.println(response.toString());
    }

    /**
     * 创建鉴权凭证
     * @param ak 租户账号对应的Access Key (AK)
     * @param sk 租户账号对应的Secret Access Key (SK)
     * @param projectId 租户账号对应的IAM项目ID
     * @return 鉴权凭证
     */
    public static ICredential getCredential(String ak, String sk, String projectId) {
        return new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
    }

    /**
     * 创建Msgsms client
     * @param region region信息
     * @param auth 鉴权凭证
     * @return MsgsmsClient实例
     */
    public static MsgsmsClient getClient(Region region, ICredential auth) {
        // 使用默认配置
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // 初始化Msgsms服务的客户端
        return MsgsmsClient.newBuilder()
                .withHttpConfig(config)
                .withCredential(auth)
                .withRegion(region)
                .build();
    }
}
